package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.http.interfaces.IntegralChangeInterface;
import com.dinadi.yidi.ui.view.ShareMenuPopupWindow;
import com.dinadi.yidi.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * author: JiaYu.He
 * Date: 2017/7/10   送礼支付结果
 * Time: 14:20
 */
public class GiftPaymentResultsActivity extends BaseActivity {
    private View view;
    private ImageView img_give_gift;
    private TextView tvTitle, tv_pay_price, tv_point;
    private String orderId, cartItemIds, giftName, coverImg;
    private int sharePosition;
    private ShareMenuPopupWindow selectImage;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(GiftPaymentResultsActivity.this).inflate(R.layout.activity_gift_payment_results, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_pay_result));
        img_give_gift = (ImageView) view.findViewById(R.id.img_give_gift);
        tv_pay_price = (TextView) view.findViewById(R.id.tv_pay_price);
        tv_point = (TextView) view.findViewById(R.id.tv_point);
    }

    @Override
    protected void initListener() {
        registerClickListener(img_give_gift);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        orderId = intent.getStringExtra("orderId");
        String totalPrice = intent.getStringExtra("totalPrice");
        tv_pay_price.setText(totalPrice);
        cartItemIds = intent.getStringExtra("cartItemIds");
        String point = intent.getStringExtra("point");
        tv_point.setText("+" + point + "积分");
        giftName = intent.getStringExtra("giftName");
        coverImg = intent.getStringExtra("coverImg");
    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.img_give_gift:
 /*分享*/
                ShowPickDialog();

                break;

            default:
                break;
        }
    }

    //弹出分享框
    private void ShowPickDialog() {
        if (selectImage == null) {
            selectImage = new ShareMenuPopupWindow(this);
            selectImage.setOnShareClickListener(shareClickListener);
        }
        selectImage.showAtLocation(this.findViewById(R.id.linearlayout_base), Gravity.BOTTOM, 0, 0);
    }

    private ShareMenuPopupWindow.OnShareClickListener shareClickListener = new ShareMenuPopupWindow.OnShareClickListener() {

        @Override
        public void onShareClick(int position) {
            screenshot(position);
        }
    };

    private void share() {
        ShareAction shareAction = new ShareAction(this);
        UMImage thumb = new UMImage(this, coverImg);
        UMWeb web = new UMWeb("http://yididesign.cn/share_order.html?order_id=" + orderId);
        web.setTitle(giftName);//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("分享内容");//描述
        if (sharePosition == 1) {
            shareAction.setCallback(umShareListener).withMedia(web).share();

        } else {
            shareAction.setCallback(umShareListener).withMedia(web).share();

        }
        switch (sharePosition) {

            case 0:
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN); // 微信好友
                break;
            case 1:
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE);// 微信朋友圈
                break;

        }
        shareAction.share();
    }

    public void screenshot(int position) {
//        if (!LoadDialog.isShowProgressDialog()) {
//            LoadDialog.showProgressDialog(getActivity(), "制作分享数据...", false, false);
        sharePosition = position;
        share();

        //   }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
            selectImage.dismiss();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            ToastUtil.showToast("分享成功啦");
            GiftPaymentResultsActivity.this.finish();
            selectImage.dismiss();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(GiftPaymentResultsActivity.this, " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
            selectImage.dismiss();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(GiftPaymentResultsActivity.this, " 分享取消了", Toast.LENGTH_SHORT).show();
            selectImage.dismiss();
        }

    };

}
